// Dungeon Manager class
// The dungeon manager's job is to store a list of z levels which link to
// to dungeons.  Later, the dungeon manager's job will expand to include
// deleting unused maps

var/const
	DUNGEON_INIT = DUNGEON_START // The first z level to use for a dungeon.
	// This is important!  All z levels after the DUNGEON_INIT
	// will be used for dungeons!  Make sure that there are never any
	// z levels in use after this level, they will be covered with a
	// a dungeon.
var/es_dungeonMan/es_dungeonMan = new()
es_dungeonMan
	var
		cur_dungeon_level = DUNGEON_INIT

		dungeons[] // list of dungeon types
	proc/Dungeon(d)
		return dungeons[d]
	proc/GetNewDungeonLevel()
		world.maxz = cur_dungeon_level
		return cur_dungeon_level ++
	New()
		dungeons = list()
		for(var/t in typesof(/es_dungeon)-/es_dungeon)
			var/es_dungeon/d = new t
			dungeons[d.id] = d
		return ..()

// Dungeon Class
// The dungeon class contains information for each individual dungeon.
es_dungeon
	var
		// This is used to identify the dungeon in staircases and such.
		id

		// type definitions to use
		wall_type = /turf/es/wall
		path_type = /turf/es/path
		room_type = /turf/es/room
		door_type = /turf/es/door

		levels = 4 // Total number of levels to generate
		height = 50
		width  = 50
		features = 700

		list/z_levels = list()
	proc/Level(n)
		if(n > z_levels.len) return null
		return z_levels[n]
	proc/Generate_Map(turf/start)
		if(z_levels.len >= levels) return
		var/es_dungeonGen/dungeon = new()
		var/nextz = es_dungeonMan.GetNewDungeonLevel()
		z_levels += nextz

		dungeon.auto_expand = TRUE
		dungeon.x0 = 1
		dungeon.y0 = 1
		dungeon.z = nextz
		dungeon.x1 = width
		dungeon.y1 = height
		dungeon.features = features
		dungeon.wall_type = wall_type
		dungeon.tunnel_type = path_type
		dungeon.room_type = room_type
		dungeon.door_type = door_type
		var/stairs
		if(start.x < width-1 && start.y < height-1)
			stairs = dungeon.Generate(locate(start.x, start.y, nextz))
		else
			stairs = dungeon.Generate()
		var/turf/es/stairs/up/u = new(stairs)
		u._toloc = start

		if(z_levels.len < levels)
			// place down stairs
			while(1)
				var/turf/t = dungeon._LocateTunnel()
				var/turf/es/stairs/down/d = new (t)
				d.id = id
				d.tolevel = z_levels.len+1
				break
		return nextz